package com.core.springSecurity.service;

import com.common.utils.Constants;
import com.common.redis.RedisCache;
import com.common.utils.EntityConversionUtil;
import com.common.utils.StringUtils;
import com.core.springSecurity.model.LoginUserDetails;
import com.core.springSecurity.model.Terminal;
import com.core.springSecurity.model.TerminalManal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: zql
 * @Description: 终端管理服务，查看在线用户，强制等出用户
 * @Data: 9:55 2021/7/23
 */
@Slf4j
@Component
public class TerminalService {
    @Resource
    RedisCache redisCache;

    @Resource
    TokenService tokenService;

    public void updateUser(LoginUserDetails loginUserDetails) {
        Long sysUserId = loginUserDetails.getUser().getSysUserId();
        TerminalManal terminalManal = redisCache.getCacheObject(Constants.TERMINAL_MANAGER_KEY);
        if (StringUtils.isNull(terminalManal)) {
            terminalManal = new TerminalManal();
            terminalManal.setMap(new HashMap<>());
        }
        Map<String, Terminal> map = terminalManal.getMap();
        Terminal terminal = EntityConversionUtil.copy(loginUserDetails, Terminal.class);
        terminal.setLoginTime(new Date(loginUserDetails.getLoginTime()));
        terminal.setExpireTime(new Date(loginUserDetails.getExpireTime()));
        terminal.setSysUserId(sysUserId);
        terminal.setFullName(loginUserDetails.getUser().getFullName());
        map.put(terminal.getLoginKey(), terminal);
        terminalManal.setLoginCount(map.size());
        redisCache.setCacheObject(Constants.TERMINAL_MANAGER_KEY, terminalManal);
    }



    public void forcedLogoutUser(String  loginKey) {
        TerminalManal terminalManal = redisCache.getCacheObject(Constants.TERMINAL_MANAGER_KEY);
        if (StringUtils.isNull(terminalManal)) {
            return;
        }
        Map<String, Terminal> map = terminalManal.getMap();
        map.remove(loginKey);
        //刷新终端管理缓存
        redisCache.setCacheObject(Constants.TERMINAL_MANAGER_KEY, terminalManal);
        //强制注销
        tokenService.delLoginUser(loginKey);
    }

    public TerminalManal getTerminal() {
        TerminalManal terminalManal = redisCache.getCacheObject(Constants.TERMINAL_MANAGER_KEY);
        if (StringUtils.isNull(terminalManal)) {
            terminalManal = new TerminalManal();
            terminalManal.setMap(new HashMap<>());
        }
        Map<String, Terminal> map = terminalManal.getMap();
        long currentTime = System.currentTimeMillis();
        //踢掉已经过期的用户
        map = map.entrySet().stream().filter(e -> currentTime < e.getValue().getExpireTime().getTime()).collect(Collectors.toMap((e) ->  e.getKey(), Map.Entry::getValue));
        terminalManal.setMap(map);
        terminalManal.setLoginCount(terminalManal.getMap().size());
        //刷新终端管理缓存
        redisCache.setCacheObject(Constants.TERMINAL_MANAGER_KEY, terminalManal);
        return terminalManal;
    }
}
